/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import beans.BairroBean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import utilidades.ConnectionFactory;

/**
 *
 * @author programador
 */

public class BairroDAO {
    
   private Connection conn; 
    
   public BairroDAO() throws Exception {
       
      try {   
         this.conn = ConnectionFactory.getConnection();
      }
      catch(Exception e) {
         throw new Exception("Erro " + e.getMessage());
      }
   
   }
   
   public List getTodosBairros() throws Exception {
       
      PreparedStatement ps = null;
      Connection connection;
      ResultSet rs = null;
      
      try {
         connection = this.conn; 
         ps = connection.prepareStatement("SELECT * FROM bairros ORDER BY nome");
         rs = ps.executeQuery();
         List<BairroBean> lista = new ArrayList<BairroBean>();
         while(rs.next()) {
            Integer id = rs.getInt(1);
            String nome = rs.getString(2);
            lista.add(new BairroBean(id, nome));
         }
         return lista;
      }
      catch(SQLException sqle) {
         throw new Exception(sqle);    
      }
      finally {
         ConnectionFactory.closeConnection(conn, ps, rs);
      }
       
   }
    
}